package com.carecenter.controller;


import ch.qos.logback.core.util.TimeUtil;
import com.carecenter.entity.Admins;
import com.carecenter.mapper.AdminsMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lgh
 * @since 2021-04-14
 */
@Data
@RestController
@RequestMapping("/admins")
public class AdminsController {

    private com.carecenter.mapper.AdminsMapper AdminsMapper;

    //主页
    @RequestMapping("/index")
    public String index() { return "index"; }

    //注册界面
    @RequestMapping("/regist")
    public String regist() { return "regist"; }

    //登录界面
    @RequestMapping("/login")
    public String login() { return "login"; }


    //用户注册
    // @RequestBody放在参数列表中你想要将json格式数据转化为相应Java对象的参数的前面；
    // @ResponseBody则放在这个方法的前面。
    @RequestMapping("/AdminsRegist")
    @ResponseBody
    public Map<String, String> AdminsRegist(@RequestBody Admins Admins) {
        Map<String, String> map = new HashMap<String, String>();
        List<Admins> list = AdminsMapper.findAll();
        for (Admins u : list) {
            if (u.getAdminName().equals(Admins.getAdminName())) {
                //System.out.println(TimeUtil.getTime()+" "+Admins.getAdminName()+" "+"注册失败");
                System.out.println(Admins.getAdminName()+" "+"注册失败");
                map.put("result", "注册失败!");
                //增加注册失败的原因反馈
                map.put("detail", "用户名已存在");
                return map;
            }
        }

        AdminsMapper.save(Admins);
        //System.out.println(TimeUtil.getTime()+" "+Admins.getAdminName()+" "+"注册成功!");
        System.out.println(Admins.getAdminName()+" "+"注册成功!");
        map.put("result", "注册成功");
        map.put("detail", "");

        return map;
    }

    //用户名是否可用
    @RequestMapping("/validateAdminsName")
    @ResponseBody
    public Map<String, String> validateAdminsName(@RequestBody Admins Admins) {
        Map<String, String> map = new HashMap<>();
        List<Admins> list = AdminsMapper.findAll();

        for (Admins u : list) {
            if(u.getAdminName().equals(Admins.getAdminName())) {
                map.put("result", "用户名已存在");
                System.out.println("aoligei");
                return map;
            }
        }

        map.put("result", "ok");
        return map;
    }

    //用户登录
    @RequestMapping("/AdminsLogin")
    @ResponseBody
    public Map<String, String> AdminsLogin(@RequestBody Admins Admins) {
        Map<String, String> map = new HashMap<String, String>();
        List<Admins> list = AdminsMapper.findAll();
        for (Admins u : list) {
            if (u.getAdminName().equals(Admins.getAdminName()) && u.getAdminPassword().equals(Admins.getAdminPassword())) {
                //System.out.println(TimeUtil.getTime() + " " +Admins.getAdminName()+" 登录成功");
                System.out.println(Admins.getAdminName()+" 登录成功");
                map.put("result", "登陆成功");
                return map;
            }
            else if (u.getAdminName().equals(Admins.getAdminName())&&!u.getAdminPassword().equals(Admins.getAdminPassword())) {
                //System.out.println(TimeUtil.getTime()+" "+Admins.getAdminName()+" 密码错误");
                System.out.println(Admins.getAdminName()+" 密码错误");
                map.put("result", "登陆失败");
                map.put("detail", "密码错误");
            }
        }
        Date date = new Date();
       // System.out.println(TimeUtil.getTime()+" "+Admins.getAdminName()+" "+"登陆失败");
        System.out.println(Admins.getAdminName()+" "+"登陆失败");
        map.put("result", "登陆失败");
        return map;
    }
}
